/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Settings;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.Interfaces.Queries;

namespace MyPhotoIndex
{
    partial class Main
    {
        public void BuildSavedQueriesMenu()
        {
            savedQueriesToolStripMenuItem.DropDownItems.Clear();
            SavedQueriesCollection savedQueries = SavedQueriesCollection.Instance;
            foreach (ISavedQuery savedQuery in savedQueries)
            {
                ToolStripItem queryMenuItem = savedQueriesToolStripMenuItem.DropDownItems.Add(savedQuery.Name);
                queryMenuItem.Tag = savedQuery;
                queryMenuItem.Click += new EventHandler(queryMenuItem_Click);
                queryMenuItem.MouseDown += new MouseEventHandler(queryMenuItem_MouseDown);
            }

            savedQueriesToolStripMenuItem.Enabled = (savedQueriesToolStripMenuItem.DropDownItems.Count > 0);
        }

        void queryMenuItem_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ToolStripMenuItem queryMenuItem = sender as ToolStripMenuItem;
                savedQueryContextMenuStrip.Tag = queryMenuItem;
                savedQueryContextMenuStrip.Show(Cursor.Position);
            }
        }

        void SavedQueryRemoveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem queryMenuItem = savedQueryContextMenuStrip.Tag as ToolStripMenuItem;
            if (queryMenuItem == null)
            {
                return;
            }

            ISavedQuery savedQuery = queryMenuItem.Tag as ISavedQuery;
            if (savedQuery == null)
            {
                return;
            }

            SavedQueriesCollection.Instance.Remove(savedQuery);
            BuildSavedQueriesMenu();
        }

        void savedQueryEditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem queryMenuItem = savedQueryContextMenuStrip.Tag as ToolStripMenuItem;
            if (queryMenuItem == null)
            {
                return;
            }

            SavedQuery savedQuery = queryMenuItem.Tag as SavedQuery;
            if (savedQuery == null)
            {
                return;
            }

            ComplexSearchForm csf = new ComplexSearchForm(savedQuery);
            csf.Owner = this;
            csf.Show();
        }
    }
}
